package com.csdj.course.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

/**
 * 请求处理之后的过滤器
 */
@Component
public class PostRequestFilter extends ZuulFilter {
    @Override
    public String filterType() {
        //过滤器的类型
        return FilterConstants.PRE_TYPE;
    }
    @Override
    public int filterOrder() {
        //过滤器的执行时机是返回值之前
        return FilterConstants.SEND_RESPONSE_FILTER_ORDER-1;
    }
    @Override
    public boolean shouldFilter() {
        //是否启用过滤器
        return true;
    }
    @Override
    public Object run() throws ZuulException {
        //核心业务代码
        RequestContext context=RequestContext.getCurrentContext();
        Long startTime= (Long)context.get("startTime");
        long duration= System.currentTimeMillis()-startTime;
        String requestURI=context.getRequest().getRequestURI();
        System.out.println("uri："+requestURI+",处理时长："+duration);
        return null;
    }
}